package com.dongdong.service.impl;

import com.dongdong.api.ItemApi;
import com.dongdong.service.CartService;
import com.dongdong.vo.CartVO;
import com.dongdong.vo.DetailVO;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ItemApi itemApi;

    @Override
    public void addCart(Long skuId, Integer num, String id) {
        //1.从redis中取出当前用户的购物车列表
        List<CartVO> cartVOS = getRedis(id);
        //1.1根据skuid查询商品信息
        val tbItem = itemApi.selectBySkuId(skuId);
        String seller = tbItem.getSeller();
        // 2.判断是否购买过当前商家的商品
        CartVO carts = null;
        for (CartVO cartVO : cartVOS) {
            if(cartVO.getSeller().equals(seller)){
                carts=cartVO;
                break;
            }
        }
        // 3.没有购买过，创建购物车并且把商品存入进去
        if (carts==null){
            //创建购物车
             CartVO cart = new CartVO();
             cart.setSeller(seller);
             //创建购物车明细
            ArrayList<DetailVO> detailVOS = new ArrayList<>();
            DetailVO detailVO = new DetailVO();
            detailVO.setItemId(tbItem.getId());
            detailVO.setNum(num);
            detailVO.setPrice(tbItem.getPrice());
            detailVO.setPicPath(tbItem.getImage());
            detailVO.setGoodsId(tbItem.getGoodsId());
            detailVO.setSellerId(tbItem.getSeller());
            detailVO.setTitle(tbItem.getTitle());
            detailVO.setTotalFee(new BigDecimal(detailVO.getNum()+"").multiply(tbItem.getPrice()));

            detailVOS.add(detailVO);
            cart.setDetailVOS(detailVOS);
            cartVOS.add(cart);
        }else{
            // 4.买过，判断是否购买过当前商家的同类型的商品
            List<DetailVO> detailVOS = carts.getDetailVOS();
            DetailVO dv = null;
            for (DetailVO detailVO : detailVOS) {
                if(detailVO.getItemId().equals(skuId)){
                    dv = detailVO;
                }
            }

            // 5.没有买过，在商品列中后追加一条记录
            if (dv ==null){
                DetailVO detailVO = new DetailVO();
                detailVO.setItemId(tbItem.getId());
                detailVO.setNum(num);
                detailVO.setPrice(tbItem.getPrice());
                detailVO.setPicPath(tbItem.getImage());
                detailVO.setGoodsId(tbItem.getGoodsId());
                detailVO.setSellerId(tbItem.getSeller());
                detailVO.setTitle(tbItem.getTitle());
                detailVO.setTotalFee(new BigDecimal(detailVO.getNum()+"").multiply(tbItem.getPrice()));
                detailVOS.add(detailVO);

            }else{
                // 6.买过，修改当前商品的数量和小计
                dv.setNum(dv.getNum()+num);
                dv.setTotalFee(new BigDecimal(dv.getNum()).multiply(dv.getPrice()));

                if(dv.getNum()==0){
                    detailVOS.remove(dv);
                }
                if(detailVOS.size()<=0){
                    cartVOS.remove(carts);
                }
            }


        }

        // 7.把新的购物车信息存入到redis中
        redisTemplate.boundHashOps(  "cartList") .put(id,cartVOS);

    }

    @Override
    public List<CartVO> getCart(String id) {
        return  getRedis(id);
    }

    //从reids 中获取当前用户的购物车信息
    public List<CartVO> getRedis(String userId) {
        //调用Redis的api查询数据
        List<CartVO> cartVOS = (List<CartVO>) redisTemplate.boundHashOps("cartList").get(userId);
        //carVOS null
        if (cartVOS == null) {
            cartVOS = new ArrayList<>();
        }
        return cartVOS;

    }








}
